package system.service.sys.impl;

import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import system.entity.SysLoginFailCounter;
import system.entity.SysLoginFailLimit;
import system.mapper.SysLoginFailCounterMapper;
import system.mapper.SysLoginFailLimitMapper;
import system.service.sys.SysLoginFailLimitService;

import java.util.List;

@Service
@CacheConfig(cacheNames = "global")
public class SysLoginFailLimitServiceImpl implements SysLoginFailLimitService {
    @Autowired
    @Getter
    private SysLoginFailLimitMapper mapper;

    @Autowired
    private SysLoginFailCounterMapper loginFailCounterMapper;

    @Override
    @CacheEvict(key = "'limit_'+#param.username")
    public Integer add(SysLoginFailLimit param) {
        return SysLoginFailLimitService.super.add(param);
    }

    @Override
    @Transactional
    @CacheEvict(key = "'limit_'+#id")
    public Integer deleteById(String id) {
        //删除限制记录
        int rows = SysLoginFailLimitService.super.deleteById(id);
        //删除统计记录
        SysLoginFailCounter counter = new SysLoginFailCounter();
        counter.setUsername(id);
        rows += loginFailCounterMapper.delete(counter);
        //
        return rows;
    }

    @Override
    @Transactional
    public Integer deleteByIds(List<String> ids) {
        int rows = 0;
        for (String id : ids) {
            rows += this.deleteById(id);
        }
        return rows;
    }

    @Override
    @Cacheable(key = "'limit_'+#id")
    public SysLoginFailLimit loadById(String id) {
        return SysLoginFailLimitService.super.loadById(id);
    }
}